package org.bing.engine.controller.preference;

import java.util.List;

import org.bing.engine.common.store.SimpleLocalStore;
import org.bing.engine.core.domain.Console;
import org.bing.engine.core.domain.ContainerInstance;
import org.bing.engine.core.domain.ControllerGroup;
import org.bing.engine.utility.helper.GuidGenerator;

public class PreferenceManager {
    private static final String KEY_GUID = "controller.guid";
    private static final String KEY_CONSOLE = "controller.console";
    private static final String KEY_GROUP = "controller.group";
    private static final String KEY_INST = "container.instance";

    private SimpleLocalStore store;

    public void setStore(SimpleLocalStore store) {
        this.store = store;
    }

    public String getControllerGuid() {
        String guid = (String) store.queryForObject(KEY_GUID);
        if (guid == null || guid.trim().length() == 0) {
            guid = GuidGenerator.generate();
            store.saveForObject(KEY_GUID, guid);
        }
        return guid;
    }

    public void saveTargetConsole(Console console) {
        store.saveForObject(KEY_CONSOLE, console);
    }

    public Console getTargetConsole() {
        return (Console) store.queryForObject(KEY_CONSOLE);
    }

    public void saveTargetGroup(ControllerGroup group) {
        store.saveForObject(KEY_GROUP, group);
    }

    public ControllerGroup getTargetGroup() {
        return (ControllerGroup) store.queryForObject(KEY_GROUP);
    }

    @SuppressWarnings("unchecked")
    public List<ContainerInstance> getContainerInstance() {
        return (List<ContainerInstance>) store.queryForObject(KEY_INST);
    }

    public void saveContainerInstance(List<ContainerInstance> instances) {
        store.saveForObject(KEY_INST, instances);
    }
}
